﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class OrderManagement_Search : PageBase
{
    private const string NEW_PAGE = "Customer_Select.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitForm();
            txtEnd.Text = DateTime.Now.ToShortDateString();
            txtStart.Text = DateTime.Now.AddMonths(-3).ToShortDateString();
            BindGrid();
        }
    }

    private void InitForm()
    {
        Jumpton.Persistent.ObjectTable ot;

        //经销商
        ddlChain.DataTextField = "DESCRIPTION";
        ddlChain.DataValueField = "CODE";
        ot = new ChainManager().GetCourseTable(this.MyStateManager.DomainID);
        if (ot != null)
        {
            ddlChain.DataSource = ot;//.FindByFilter("IsValid='true'");
            ddlChain.DataBind();
        }
        ddlChain.Items.Insert(0, new ListItem("-所有-", ""));


        DictionaryItemsManager dim = new DictionaryItemsManager();

        //区域层次
        ddlLevel.DataTextField = "Name";
        ddlLevel.DataValueField = "DictionaryItemId";
        ddlLevel.DataSource = dim.GetItemList(DictionaryEnum.ChannelType, this.MyStateManager.DomainID);
        ddlLevel.DataBind();
        ddlLevel.Items.Insert(0, new ListItem("--所有--", ""));

        //客户类别
        ddlClass.DataTextField = "Name";
        ddlClass.DataValueField = "DictionaryItemId";
        ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new ListItem("-所有-", ""));

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("-所有-", ""));

        //巡店督导
        DataTable dt = new Jumpton.SFA.Server.BusinessRule.MobileUserManager().Load("", this.MyStateManager.DomainID,this.MyStateManager.OrgID);
        ddlUser.DataTextField = "CODENAME";
        ddlUser.DataValueField = "ID";
        ddlUser.DataSource = dt;
        ddlUser.DataBind();
        ddlUser.Items.Insert(0, new ListItem("--所有--", ""));

        //绑定订单状态
        string[] names = Enum.GetNames(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        int[] values = (int[])Enum.GetValues(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        for (int i = 0; i < names.Length; i++)
        {
            ddlStatus.Items.Add(new ListItem(names[i], values[i].ToString()));
        }
        ddlStatus.Items.Insert(0, new ListItem("--所有--", ""));

        names = Enum.GetNames(typeof(Jumpton.SFA.Server.Entity.EOrderType));
        values = (int[])Enum.GetValues(typeof(Jumpton.SFA.Server.Entity.EOrderType));
        for (int i = 0; i < names.Length; i++)
        {
            ddlOrderType.Items.Add(new ListItem(names[i], values[i].ToString()));
        }
        ddlOrderType.Items.Insert(0, new ListItem("--所有--", ""));
    }

    private void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(" ");


        if (ddlChain.SelectedValue != string.Empty)
        {
            sb.Append(" And C.CHAIN_CODE='" + ddlChain.SelectedValue + "' ");
        }
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }
        if (ddlClass.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
        }
        if (ddlLevel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.GEO_LEVEL_CODE ='" + ddlLevel.SelectedValue + "' ");
        }
        if (ddlUser.SelectedValue != string.Empty)
        {
            sb.Append(" AND OH.USER_ID ='" + ddlUser.SelectedValue + "'  ");
        }
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND (C.CODE LIKE '%" + txtCode.Text + "%' OR C.NAME LIKE '%" + txtCode.Text + "%')  ");
        }
        if (txtStart.Text != string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,OH.ORDER_date,'" + DateTime.Parse(txtStart.Text).ToShortDateString() + "')<=0 ");
        }
        if (txtEnd.Text != string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,OH.ORDER_date,'" + DateTime.Parse(txtEnd.Text).ToShortDateString() + "')>=0 ");
        }
        if (ddlStatus.SelectedValue != string.Empty)
        {
            sb.Append(" AND  OH.STATUS='" + ddlStatus.SelectedValue + "'  ");
        }
        if (this.ddlOrderType.SelectedValue != string.Empty)
        {
            sb.Append(" AND  OH.OrderType='" + ddlOrderType.SelectedValue + "'  ");
        }
        if (this.drpCancelled.SelectedValue != string.Empty)
        {
            //有效
            sb.Append(string.Format(" AND OH.CANCELLED='{0}' ", drpCancelled.SelectedValue));
        }

        DataSet ds = OrderHeaderManager.SearchOrderList(MyStateManager.DomainID, sb.ToString());
        if (ds != null && ds.Tables.Count > 0)
        {
            gvChannelType.DataSource = ds.Tables[0];
            gvChannelType.DataBind();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect(NEW_PAGE);
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[9].Text.IndexOf("Y") > -1)
            {
                e.Row.Cells[9].Text = "无效";
            }
            else
            {
                if (e.Row.Cells[9].Text.IndexOf("N") > -1)
                {
                    e.Row.Cells[9].Text = "有效";
                }
            }
        }
    }

}
